package top.aylan.org.ctrl.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import top.aylan.org.Common.SystemConstants;
import top.aylan.org.entity.User;
import top.aylan.org.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/12/9.
 */
@RequestMapping("/admin")
@Controller
public class AdminAuthController {

    @Resource
    UserService userService;

    @RequestMapping(value = "/login")
    public String login(){

        return "admin/login";
    }

    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String doLogin(
            HttpServletRequest request,
            Model model,
            @RequestParam(value = "account",defaultValue = "") String account,
            @RequestParam(value = "password",defaultValue = "") String password
    ){

        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        try{
            userService.login(account,password,request);
            request.getSession().setAttribute(SystemConstants.ADMIN_AUTH_USER,user);
            return "redirect:/admin/index";
        }catch(Exception e){
            model.addAttribute("msg",e.getMessage());
            return "admin/login";
        }
    }
}
